package com.ajou.adonate.servlet;

import com.ajou.adonate.database.*;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class StoreOAuthToken
 */
@WebServlet("/StoreOAuthToken")
public class StoreOAuthToken extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StoreOAuthToken() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		DatabaseManager d = new DatabaseManager();
		try {
			Class.forName(d.getJdbcDriver());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		String user_email = request.getParameter("user");
		String oauth_token = request.getParameter("oauth_token");
		String oauth_token_secret = request.getParameter("oauth_token_secret");
		
		int account_id = d.getAccountId(user_email);
		
		if(account_id == -1) {
			d.insertAccount(user_email, oauth_token, oauth_token_secret);
		} else {
			d.updateAccount(user_email, oauth_token, oauth_token_secret, account_id);
		}
		
		System.out.println("user : " + user_email);
		System.out.println("oauth_token : " + oauth_token);
		System.out.println("oauth_token_secret : " + oauth_token_secret);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
